package github.kawaiior.botapiserver.controller;

import github.kawaiior.botapiserver.common.ActionResponse;
import github.kawaiior.botapiserver.service.UserService;
import github.kawaiior.botapiserver.util.CaptchaUtil;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/admin")
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    private static final ActionResponse CAPTCHA_ERROR = new ActionResponse(0, "验证码错误");

    @RequestMapping("/update_password")
    public ActionResponse adminUpdatePassword(String account, String password, String newPassword, String code, HttpSession session){
        // 检查验证码
        if (CaptchaUtil.codeError(code, session)){
            return CAPTCHA_ERROR;
        }
        return userService.adminUpdatePassword(account, password, newPassword);
    }

    @RequestMapping("/get_bots")
    public ActionResponse getBots(){
        return userService.getBots();
    }

    @RequestMapping("/get_bot_info")
    public ActionResponse getBotInfo(String bot_id){
        return userService.getBotInfo(bot_id);
    }

    @RequestMapping("/get_bot_keywords")
    public ActionResponse getBotKeywords(String bot_id){
        return userService.getBotKeywords(bot_id);
    }

    @RequestMapping("/add_keyword_to_bot")
    public ActionResponse addKeywordToBot(String bot_id, String word){
        return userService.addKeywordToBot(bot_id, word);
    }

    @RequestMapping("/remove_bot_keyword")
    public ActionResponse removeBotKeyword(String bot_id, String word){
        return userService.removeBotKeyword(bot_id, word);
    }

    @RequestMapping("/get_bot_groups")
    public ActionResponse getBotGroups(String bot_id){
        return userService.getBotGroups(bot_id);
    }

    @RequestMapping("/get_group_users")
    public ActionResponse getGroupUsers(String bot_id, String group_id){
        return userService.getGroupUsers(bot_id, group_id);
    }

    @RequestMapping("/add_user_to_group")
    public ActionResponse addUserToGroup(String bot_id, String group_id, String user_id){
        return userService.addUserToGroup(bot_id, group_id, user_id);
    }

    @RequestMapping("/remove_group_user")
    public ActionResponse removeGroupUser(String bot_id, String group_id, String user_id){
        return userService.removeGroupUser(bot_id, group_id, user_id);
    }

    @RequestMapping("/get_bot_group_user_ips")
    public ActionResponse getBotGroupUserIps(String bot_id, String group_id, String user_id){
        return userService.getBotGroupUserIps(bot_id, group_id, user_id);
    }

    @RequestMapping("/add_ip_to_bot_user")
    public ActionResponse addIpToBotUser(String bot_id, String group_id, String user_id, String ip){
        return userService.addIpToBotUser(bot_id, group_id, user_id, ip);
    }

    @RequestMapping("/remove_bot_user_ip")
    public ActionResponse removeBotUserIp(String bot_id, String group_id, String user_id, String ip){
        return userService.removeBotUserIp(bot_id, group_id, user_id, ip);
    }

    @RequestMapping("/update_bot")
    public ActionResponse updateBotAtConfig(String bot_id, int req_at, int resp_at){
        return userService.updateBotAtConfig(bot_id, req_at, resp_at);
    }

}
